package cn.zongtilei;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.isever.IBanjiService;
import cn.lei.Banji;
import cn.lei.PageBean2;
import cn.severimpl.BanjiServiceImpl;

/**
 * Servlet implementation class BanjiServlet
 */
public class BanjiServlet extends HttpServlet {
	private IBanjiService banjiservice = new BanjiServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");	//post方法解决乱码
		String method = req.getParameter("method");
		switch (method) {
		case "list":
			list(req, resp);
			break;
		case "insert":
			//从浏览器获取值，然后封装到对象里         
			insert(req, resp);
			break;
		case "pageList":
			pageList(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "toupdate":
			toupdate(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		default:
			break;
		}
		
	}
	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer id = Integer.parseInt(req.getParameter("id"));
		String name = req.getParameter("name");
		Integer number = Integer.parseInt(req.getParameter("number"));
		Banji banji = new Banji(id,name,number);
		boolean result = banjiservice.update(banji);
		System.out.println(result?"成功":"失败");
		resp.sendRedirect(req.getContextPath()+"/banji?method=pageList");
	}
	private void toupdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Integer id = Integer.parseInt(req.getParameter("id"));
		Banji banji = banjiservice.findById(id);
		req.setAttribute("banji", banji);
		req.getRequestDispatcher("/banji_update.jsp").forward(req, resp);
	}
	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer id = Integer.parseInt(req.getParameter("id"));
		boolean result = banjiservice.deleteById(id);
		System.out.println(result);
		resp.sendRedirect(req.getContextPath()+"/banji?method=pageList");
	}
	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pagenomber = req.getParameter("pageNo");
		if (pagenomber == null || pagenomber.equals("")) {
			pagenomber = "1";
		}

		int pageNo = Integer.parseInt(pagenomber);
		String pageSi = req.getParameter("pageSize");
		if (pageSi == null || pageSi.equals("")) {
			pageSi = "2";
		}
		int pageSize = Integer.parseInt(pageSi);
		// 2.封装成pageBean，调用service层业务逻辑
	
		//2.到service层处理
		PageBean2 pageBean2 = banjiservice.getpageBean(pageNo,pageSize);
		System.out.println(pageBean2);
		//3.转发重定向
		req.setAttribute("pageBean2", pageBean2);
		req.getRequestDispatcher("banji_file.jsp").forward(req, resp);
	}
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		Integer number = Integer.parseInt(req.getParameter("number"));
		Banji banji = new Banji(name, number);
		//调用service
		boolean result = banjiservice.insert(banji);
		System.out.println(result ? "成功" : "失败");
		//重定向
		resp.sendRedirect(req.getContextPath()+"/banji?method=pageList");
	}
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Banji> list = banjiservice.list();
		for (Banji banji : list) {
			//System.out.println(banji);
		}
		//保存数据
		req.setAttribute("list", list);
		req.getRequestDispatcher("/banji_file.jsp").forward(req, resp);
	}
}
